package com.md5;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

public class MD5Encode {
	private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
		"6", "7", "8", "9", "a", "b", "c", "d", "e", "f" };

	/**
	 * 转换字节数组为16进制字串
	 * 
	 * @param b
	 *            字节数组
	 * @return 16进制字串
	 */
	
	private static String byteArrayToHexString(byte[] b) {
		StringBuffer resSb = new StringBuffer();
		for (int i = 0; i < b.length; i++) {
			resSb.append(byteToHexString(b[i]));
		}
		return resSb.toString();
	}
	
	private static String byteToHexString(byte b) {
		int n = b;
		if (n < 0)
			n = 256 + n;
		int d1 = n / 16;
		int d2 = n % 16;
		return hexDigits[d1] + hexDigits[d2];
	}

	public static String getMD5Str(String str) {
		String resStr = "";
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			resStr = byteArrayToHexString(md.digest(str.getBytes()));
		} catch (Exception ex) {
			
		}
		return resStr;
	}

	public static String encodePWD(String userId,String pwd){
		return getMD5Str(userId.toUpperCase()+":"+pwd);
	}
	
  public static String getFileMD5(String fn) {
    return getFileMD5(new File(fn));
  }
	
	public static String getFileMD5(File file) {
    FileInputStream fis = null;
    try {
      MessageDigest md = MessageDigest.getInstance("MD5");
      fis = new FileInputStream(file);
      byte[] buffer = new byte[8192];
      int length = -1;
      while ((length = fis.read(buffer)) != -1) {
        md.update(buffer, 0, length);
      }
      fis.close();
      return byteArrayToHexString(md.digest());
    } catch (Exception ex) {
      return null;
    } 
	}
	
	//test code
	private static void test1(){
		String userId = "dzy";
		String pwd = "dzy";
		System.out.println("user=dzy,pwd=dzy,md5 pwd="+ getMD5Str(userId.toUpperCase()+":"+pwd));

		userId = "user1";
		pwd = "user1";
		System.out.println("user=user1,pwd=user1,md5 pwd="+ getMD5Str(userId.toUpperCase()+":"+pwd));
		System.out.println("user=superuser,pwd=123,md5 pwd="+ getMD5Str("superuser".toUpperCase()+":"+"123"));
		System.out.println("user=dzy,pwd=aaa123,md5 pwd="+ getMD5Str("dzy".toUpperCase()+":"+"aaa123"));
    System.out.println("user=guolinlin,pwd=apex123,md5 pwd="+ getMD5Str("guolinlin".toUpperCase()+":"+"apex123"));
    System.out.println("user=haibo,pwd=aaa123,md5 pwd="+ getMD5Str("haibo".toUpperCase()+":"+"aaa123"));
    System.out.println("user=陈泽龙,pwd=aaa123,md5 pwd="+ getMD5Str("陈泽龙".toUpperCase()+":"+"aaa123"));
    System.out.println("user=1993,pwd=1993,md5 pwd="+ getMD5Str("1993".toUpperCase()+":"+"1993"));
    System.out.println("user=user1,pwd=user1,md5 pwd="+ getMD5Str("user1".toUpperCase()+":"+"user1"));
    System.out.println("user=1466,pwd=user2,md5 pwd="+ getMD5Str("1466".toUpperCase()+":"+"user2"));
    System.out.println("user=3308,pwd=user1,md5 pwd="+ getMD5Str("3308".toUpperCase()+":"+"user1"));
    System.out.println("user=3757,pwd=chris,md5 pwd="+ getMD5Str("3757".toUpperCase()+":"+"chris"));
    System.out.println("user=3763,pwd=112233,md5 pwd="+ getMD5Str("3763".toUpperCase()+":"+"112233"));
    System.out.println("user=4189,pwd=112233,md5 pwd="+ getMD5Str("4189".toUpperCase()+":"+"112233"));
    System.out.println("user=4192,pwd=112233,md5 pwd="+ getMD5Str("4192".toUpperCase()+":"+"112233"));
    System.out.println("user=4195,pwd=112233,md5 pwd="+ getMD5Str("4195".toUpperCase()+":"+"112233"));
    System.out.println("user=4198,pwd=112233,md5 pwd="+ getMD5Str("4198".toUpperCase()+":"+"112233"));
    System.out.println("user=4243,pwd=112233,md5 pwd="+ getMD5Str("4243".toUpperCase()+":"+"112233"));
    System.out.println("user=4143,pwd=1,md5 pwd="+ getMD5Str("4143".toUpperCase()+":"+"1"));
    System.out.println("user=3828,pwd=1,md5 pwd="+ getMD5Str("3828".toUpperCase()+":"+"1"));
    System.out.println("user=4163,pwd=112233,md5 pwd="+ getMD5Str("4163".toUpperCase()+":"+"112233"));
    System.out.println("user=1(superuser),pwd=123456,md5 pwd="+ getMD5Str("1".toUpperCase()+":"+"123456"));
    System.out.println("user=6500(xuhao),pwd=xuhao123,md5 pwd="+ getMD5Str("6500".toUpperCase()+":"+"xuhao123"));
    System.out.println("user=6508(langlang),pwd=langlang123,md5 pwd="+ getMD5Str("6508".toUpperCase()+":"+"langlang123"));
    System.out.println("user=shcz,pwd=apex123,md5 pwd="+ getMD5Str("shcz".toUpperCase()+":"+"apex123"));
    System.out.println("user=beidayiyuan,pwd=apex123,md5 pwd="+ getMD5Str("beidayiyuan".toUpperCase()+":"+"apex123"));
    System.out.println("user=ootc,pwd=apex123,md5 pwd="+ getMD5Str("ootc".toUpperCase()+":"+"apex123"));
    System.out.println("user=rj,pwd=rj123,md5 pwd="+ getMD5Str("rj".toUpperCase()+":"+"rj123"));
    System.out.println("user=zshospital,pwd=123,md5 pwd="+ getMD5Str("zshospital".toUpperCase()+":"+"123"));
    
    
    
    
    System.out.println("schx pwd="+getMD5Str("schx".toUpperCase()+":"+"schx"));
	}
	/*
	private static void execPWD(){
	  ResultSet rs = null;
    Connection con = null;
    List<String> sqlList = new ArrayList<String>();
    try
    {
      con = DBMgr.inst().getConn();
      rs = DBUtil.runSQLToRS("select * from right_user", con);
      while (rs.next())
      {
        String userId = rs.getString("userid");
        String name = rs.getString("name");
        String pwd = "";
        if(name.endsWith("A") || name.endsWith("B") || name.endsWith("C")||name.endsWith("D")){
          pwd = "cotrs123";
        } 
        else if(name.startsWith("px")){
          pwd = "112233";
        }
        if(StringUtils.isEmpty(pwd)){
          continue;
        }
        System.out.println(name);
        sqlList.add("update right_user set password = '"+getMD5Str(userId+":"+pwd)+"' where userid="+userId);
      }
      DBUtil.runBatchSQL(sqlList);
    }
    catch (Exception e)
    {
      String sMsg = PTK.getExceptionFullStr(e);
      System.out.println(sMsg);
    }
    finally
    {
      DBMgr.closeDBRes(rs, con);
    }
	  
	}
	*/

	public static void main(String[] args) {
//		test1();
//		System.out.println(getMD5Str("drc051218"));
//		execPWD();
	  //System.out.println(getMD5Str("mohwst:sjgv6562"));
	  
	  System.out.println("10001="+encodePWD("10001","14452943"));//58d3dca5553ed4c0efd1cbd519778d40
	  System.out.println("1="+encodePWD("1","cltr112233"));//58d3dca5553ed4c0efd1cbd519778d40
    //System.out.println("7765="+encodePWD("7765","112233"));
//	  System.out.println( getMD5Str("auditpf_:41:"));
	}
}
